package com.winit.openapi.ups.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.command.IdCommand;
import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ups.spi.systemrule.CityService;
import com.winit.ups.spi.systemrule.CountryService;
import com.winit.ups.spi.systemrule.DistrictService;
import com.winit.ups.spi.systemrule.StateService;
import com.winit.ups.spi.systemrule.command.CityCommand;
import com.winit.ups.spi.systemrule.command.CountryCheckedCommand;
import com.winit.ups.spi.systemrule.command.CountryCommand;
import com.winit.ups.spi.systemrule.command.DistrictCommand;
import com.winit.ups.spi.systemrule.command.QueryItemsCommand;
import com.winit.ups.spi.systemrule.command.StateCommand;
import com.winit.ups.spi.systemrule.vo.City;
import com.winit.ups.spi.systemrule.vo.Country;
import com.winit.ups.spi.systemrule.vo.District;
import com.winit.ups.spi.systemrule.vo.DistrictItems;
import com.winit.ups.spi.systemrule.vo.State;

/**
 * 国家省市区 <Change to the actual description of this class>
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * pan.zhou.zeng 	1.0  		2017年04月27日 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/ups2/address")
public class UpsAddressController extends BaseController {

    @Resource
    private CountryService  upsCountryService;
    @Resource
    private StateService    upsStateService;
    @Resource
    private CityService     upsCityService;
    @Resource
    private DistrictService upsDistrictService;

    /**
     * 根据countrycode获得省份
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/province")
    public String listProvinceByCountryCode(@ModelAttribute RequestMsg requestMsg,
                                            @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数s
        IdCommand<String> command = JSON.parseObject(requestMsg.getData().toString(),IdCommand.class);
        List<State> stateList = upsStateService.queryStatesByCountryCode(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", stateList);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 根据provinceCode获得城市
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/city")
    public String listCityByProvinceCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        // 获取参数
        CityCommand command = JSON.parseObject(requestMsg.getData().toString(),CityCommand.class);
        List<City> cityList = upsCityService.getCityByStateId(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", cityList);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 根据cityCode获得区域和邮编
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/district")
    public String listDistrictByCityCode(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        // 获取参数
        QueryItemsCommand command = JSON.parseObject(requestMsg.getData().toString(),QueryItemsCommand.class);
        List<DistrictItems> districtList = upsDistrictService.findDistrictItems(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", districtList);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    
    /**
     * 查询所有的国家
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/allCountry")
    public String queryAllCountrys(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	
    	CountryCommand countryCommand=new CountryCommand();
        //为请求参数设置上下文
        countryCommand.setCtx(CommandContext.getContext());      
    	List<Country>  countryList=upsCountryService.getCountryList(countryCommand);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", countryList);
        responseMsg.setData(data);
        return SUCCESS;
    }
    

}
